<?php

namespace app\http\middleware;

use app\facade\Token;
use think\Db;
use app\models\{User as Users, UserLevel, Riderlist};
use app\facade\Redis;

class Auth
{
    use \traits\controller\Jump;

    public function handle($request, \Closure $next)
    {
        $url = '/' . $request->controller() . '/' . $request->action();
        $url = strtolower($url);
        $verify = config('auth.api');
        if (!in_array($url, $verify) && !$request->header('token') && $request->controller() != 'Rider') {
            $this->result(null, 999, '请授权登陆账号');
        }
        if (!in_array($url, $verify) && empty($request->header('tokens')) && $request->controller() == 'Rider') {
            $this->result(null, 995, '请登录账号');
        }

        $ip = $request->ip();
        $code = $ip;
        if ($request->controller() != 'Rider') {
            $userlevel = (new UserLevel())->field('zhekou,money,contents,protocol,isopen')->where('level', 2)->find();
            $request->userlevel = $userlevel;
            $userlevel['zhekou'] = (float)$userlevel['zhekou'];
            if ($userlevel['zhekou'] <= 0 || $userlevel['zhekou'] >= 100) {
                $userlevel['zhekou'] = 100;
            }
            $request->user_dict = $userlevel['zhekou'] ?? 100; //会员折扣价
        }
        //用户端
        if (($request->header('token') && !in_array($url, $verify)) && $request->controller() != 'Rider') {
            $user_id = Token::get($request->header('token'));
            $user = (new Users())->where('id', $user_id)->find();
            $code = $user_id;
            if (!$user) {
                $this->result(null, 999, '用户账号错误');
            }
            $request->user = $user;
            $request->user_id = $user_id;
        } elseif (($request->header('token') && in_array($url, $verify)) && $request->controller() != 'Rider') {
            $user_id = Token::get($request->header('token'));
            $user = (new Users())->where('id', $user_id)->find();
            $code = $user_id;
            $request->user = $user;
            $request->user_id = $user_id;
        }
        //师傅端
        if (($request->header('tokens') && !in_array($url, $verify)) && $request->controller() == 'Rider') {
            $user_id = Token::get($request->header('tokens'));
            $prefix = substr($request->header('tokens'), 0, 5);
            if ($prefix == 'RIDER') {
                $user = (new Riderlist())->where('id', $user_id)->find();
            } else {
                $this->result(null, 995, '账号错误');
            }
            $code = $user_id;
            if (!$user) {
                $this->result(null, 995, '账号错误');
            }
            $request->reder = $user;
            $request->reder_id = $user_id;
        }
        if ($request->isPost() && $url != '/other/oss' && $url != '/user/cart') {
            $unique = date('YmdHi') . '_' . $url . '_' . $code;
            $frtimes = Redis::get($unique);
            if ($frtimes) {
                $this->result(null, 404, '操作频繁，请重试');
            } else {
                Redis::setex($unique, 1, 1);
            }
        }
        return $next($request);
    }
}
